/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import top.continew.admin.hanfu.domain.Active;
import top.continew.admin.hanfu.domain.ActiveTable;
import top.continew.admin.hanfu.domain.TableColumn;
import top.continew.admin.hanfu.domain.TableValue;
import top.continew.admin.hanfu.domain.dto.TableValueDto;
import top.continew.admin.hanfu.service.impl.ActiveServiceImpl;
import top.continew.admin.hanfu.service.impl.ActiveTableServiceImpl;
import top.continew.admin.hanfu.service.impl.TableColumnServiceImpl;
import top.continew.admin.hanfu.service.impl.TableValueServiceImpl;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-03-31
 */
@RestController
@RequestMapping("/table_value")
@RequiredArgsConstructor
public class TableValueController {

    private final TableValueServiceImpl tableValueService;

    private final ActiveServiceImpl activeService;

    private final ActiveTableServiceImpl activeTableService;

    private final TableColumnServiceImpl columnService;

    @SaIgnore
    @PostMapping("")
    public R registerActive(@RequestBody TableValueDto valueDto) {

        Active byId = activeService.getById(valueDto.getActiveId());

        ActiveTable one = activeTableService.lambdaQuery().eq(ActiveTable::getActiveId, valueDto.getActiveId()).one();

        Long count = tableValueService.lambdaQuery().eq(TableValue::getTableId, one.getId()).count();

        if (byId.getActiveLimit() <= count) {
            return R.fail("报名人数已满");
        }

        TableValue tableValue = BeanUtil.copyProperties(valueDto, TableValue.class);

        tableValue.setTableId(one.getId());

        tableValue.setUpdateTime(System.currentTimeMillis() + "");

        boolean save = tableValueService.save(tableValue);

        return save ? R.success() : R.fail("报名失败");
    }

    @GetMapping("/{activeId}")
    public R<List<TableValue>> getTableValue(@PathVariable("activeId") Integer activeId) {

        ActiveTable one = activeTableService.lambdaQuery().eq(ActiveTable::getActiveId, activeId).one();

        List<TableValue> list = tableValueService.lambdaQuery().eq(TableValue::getTableId, one.getId()).list();

        return R.success(list);
    }

    @GetMapping("/head/{activeId}")
    public R<List<TableColumn>> getTableHead(@PathVariable("activeId") Integer activeId) {

        ActiveTable one = activeTableService.lambdaQuery().eq(ActiveTable::getActiveId, activeId).one();

        List<TableColumn> list = columnService.lambdaQuery().eq(TableColumn::getTableId, one.getId()).list();

        return R.success(list);
    }

}
